<template>
  <div class="counter">
    <div class="counter-header">
      <h2>计数器示例</h2>
      <p>展示Pinia的基本用法：state、getters、actions</p>
    </div>

    <div class="counter-main">
      <div class="counter-display">
        <div
          class="count-value"
          :class="{ even: counterStore.isEven, odd: !counterStore.isEven }"
        >
          {{ counterStore.count }}
        </div>
        <div class="count-info">
          <div class="info-item">
            <span class="label">双倍值:</span>
            <span class="value">{{ counterStore.doubleCount }}</span>
          </div>
          <div class="info-item">
            <span class="label">奇偶性:</span>
            <span class="value">{{
              counterStore.isEven ? '偶数' : '奇数'
            }}</span>
          </div>
        </div>
      </div>

      <div class="counter-controls">
        <button @click="counterStore.decrement" class="btn btn-danger">
          -1
        </button>
        <button @click="counterStore.increment" class="btn btn-primary">
          +1
        </button>
        <button @click="counterStore.incrementBy(5)" class="btn btn-success">
          +5
        </button>
        <button @click="counterStore.reset" class="btn btn-warning">
          重置
        </button>
      </div>
    </div>

    <div class="counter-history">
      <div class="history-header">
        <h3>操作历史 ({{ counterStore.historyCount }}/10)</h3>
        <button
          @click="counterStore.clearHistory"
          class="btn btn-small btn-secondary"
        >
          清空历史
        </button>
      </div>

      <div class="history-list">
        <div v-if="counterStore.history.length === 0" class="history-empty">
          暂无操作历史
        </div>
        <div v-else>
          <div
            v-for="(item, index) in counterStore.history"
            :key="index"
            class="history-item"
          >
            <div class="history-action">{{ item.action }}</div>
            <div class="history-count">{{ item.count }}</div>
            <div class="history-time">{{ item.timestamp }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useCounterStore } from '@/stores/counter'

export default {
  name: 'Counter',
  setup() {
    const counterStore = useCounterStore()

    return {
      counterStore
    }
  }
}
</script>

<style scoped>
.counter {
  max-width: 800px;
  margin: 0 auto;
}

.counter-header {
  text-align: center;
  margin-bottom: 3rem;
  background: rgba(255, 255, 255, 0.1);
  padding: 2rem;
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.counter-header h2 {
  color: white;
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.counter-header p {
  color: rgba(255, 255, 255, 0.8);
  font-size: 1.1rem;
}

.counter-main {
  background: rgba(255, 255, 255, 0.95);
  padding: 3rem;
  border-radius: 20px;
  margin-bottom: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.counter-display {
  text-align: center;
  margin-bottom: 3rem;
}

.count-value {
  font-size: 6rem;
  font-weight: 700;
  margin-bottom: 1rem;
  transition: all 0.3s ease;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.count-value.even {
  color: #4caf50;
}

.count-value.odd {
  color: #ff5722;
}

.count-info {
  display: flex;
  justify-content: center;
  gap: 3rem;
}

.info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.info-item .label {
  color: #666;
  font-size: 0.9rem;
  font-weight: 500;
}

.info-item .value {
  color: #333;
  font-size: 1.2rem;
  font-weight: 600;
}

.counter-controls {
  display: flex;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.btn {
  padding: 1rem 2rem;
  border: none;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 100px;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.btn-danger {
  background: linear-gradient(135deg, #ff6b6b, #ee5a24);
  color: white;
}

.btn-success {
  background: linear-gradient(135deg, #4ecdc4, #44a08d);
  color: white;
}

.btn-warning {
  background: linear-gradient(135deg, #ffeaa7, #fdcb6e);
  color: #333;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-small {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  min-width: auto;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.counter-history {
  background: rgba(255, 255, 255, 0.95);
  padding: 2rem;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid #f0f0f0;
}

.history-header h3 {
  color: #333;
  font-size: 1.5rem;
}

.history-list {
  max-height: 300px;
  overflow-y: auto;
}

.history-empty {
  text-align: center;
  color: #999;
  padding: 2rem;
  font-style: italic;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 1rem;
  padding: 1rem;
  background: #f8f9fa;
  margin-bottom: 0.5rem;
  border-radius: 10px;
  align-items: center;
}

.history-action {
  font-weight: 600;
  color: #333;
}

.history-count {
  font-weight: 700;
  color: #667eea;
  font-size: 1.1rem;
}

.history-time {
  color: #666;
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .counter-main {
    padding: 2rem;
  }

  .count-value {
    font-size: 4rem;
  }

  .count-info {
    gap: 2rem;
  }

  .counter-controls {
    gap: 0.5rem;
  }

  .btn {
    padding: 0.8rem 1.5rem;
    font-size: 1rem;
    min-width: 80px;
  }

  .history-item {
    grid-template-columns: 1fr;
    gap: 0.5rem;
    text-align: center;
  }
}
</style>
